using System.IO;
using System.Text;
using UnityEditor;
using UnityEditor.ProjectWindowCallback;
using System;
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;

public class ViewCreateScript : EndNameEditAction
{
    public static string objName { set; get; }
    public static string uiPathName { set; get; }

    public override void Action(int instanceId, string pathName, string resourceFile)
    {
        string text = File.ReadAllText(resourceFile);
        string className = Path.GetFileNameWithoutExtension(pathName);
        string tab = "   ";
        string crlName = className + "Ctrl";
        string varName = crlName.Substring(0, 1).ToLower() + crlName.Substring(1, crlName.Length - 1);
        varName = varName.Replace("UI", "");

        className = className.Replace(" ", "");


        text = "using FutureCore;\n";
        text += "using UnityEngine.UI;\n";
        text += "using UnityEngine;\n\n";
        text += "public class " + className + " : BaseUI\n{\n" + tab + "";
        text += "#region BaseUI\n" + tab + "" + crlName + " ctrl;\n\n" + tab + "";
        text += "public " + className + "(BaseUICtrl ctrl) : base(ctrl)\n" + tab + "{\n" + tab + tab + "uiName=UIConst."+className  +";\n"+ tab+"}\n\n" + tab + "";
        text += "protected override void SetUIInfo(UIInfo uiInfo)\n" + tab + "{\n" + tab + "" + tab + "uiInfo.assetPath =\"" + uiPathName + "\";\n" + tab + "" + tab + "uiInfo.layer = UILayer.Normal;\n" + tab + "" + tab + "uiInfo.isStaticUI = false;\n" + tab + "}\n\n" + tab + "";
        text += "protected override void OnInit()\n" + tab + "{\n" + tab + tab + "ctrl = baseCtrl as " + crlName + ";\n" + tab + "}\n\n" + tab + "";
        text += "protected override void OnBind()\n" + tab + "{\n" + tab + "" + tab + "BindUICom();\n" + tab + "}\n\n" + tab + "";
        text += "protected override void OnOpenBefore(object args)\n" + tab + "{\n" + tab + "}\n\n" + tab + "";
        text += "protected override void OnOpen(object args)\n" + tab + "{\n" + tab + "}\n\n" + tab + "";
        text += "protected override void OnClose()\n" + tab + "{\n" + tab + "}\n\n" + tab + "";
        text += "protected override void OnDisplay(object args)\n" + tab + "{\n" + tab + "}\n\n" + tab + "";
        text += "protected override void OnHide()\n" + tab + "{\n" + tab + "}\n\n" + tab + "";
        text += "protected override void AddListener()\n" + tab + "{\n" + tab + "}\n\n" + tab + "";
        text += "protected override void RemoveListener()\n" + tab + "{\n" + tab + "}\n" + tab + "";
        text += "#endregion\n\n";
        text += tab + "#region Bind\n";
        text += tab + "private void BindUICom()\n" + tab + "{\n" + tab + "}\n";
        text += tab + "#endregion\n";
        text += "}";

        var encoding = new UTF8Encoding(true, false);

        File.WriteAllText(pathName, text, encoding);

        AssetDatabase.ImportAsset(pathName);
        var asset = AssetDatabase.LoadAssetAtPath<MonoScript>(pathName);
        ProjectWindowUtil.ShowCreatedAsset(asset);
    }
}